/*DONE* Table [dbo].[<CompanyData>$Offer$5ecfc871-5d82-43f1-9c54-59685e82318d]  1/1  Script Date: 29.7.2020 - JI ******/
/*Add prestaging table for all companies in LSInsight$Companies table*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyCounter as INT
DECLARE @i as INT

SET @i = 0
SET @CompanyCounter = (SELECT MAX([Company]) FROM LSInsight$Companies)

WHILE @i <= @CompanyCounter 
BEGIN
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @i)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Offer$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Offer$5ecfc871-5d82-43f1-9c54-59685e82318d](
	[timestamp] [timestamp] NOT NULL,
	[No_] [nvarchar](20) NOT NULL,
	[Type] [int] NOT NULL,
	[Description] [nvarchar](30) NOT NULL,
	[Status] [int] NOT NULL,
	[Offer Type] [int] NOT NULL,
	[Price Group] [nvarchar](10) NOT NULL,
	[Disc_ _ from Std_ Price] [decimal](38, 20) NOT NULL,
	[Rounding Method] [nvarchar](10) NOT NULL,
	[Currency Code] [nvarchar](10) NOT NULL,
	[Last Date Modified] [datetime] NOT NULL,
	[No_ Series] [nvarchar](10) NOT NULL,
	[Validation Period ID] [nvarchar](10) NOT NULL,
	[Block Periodic Discount] [tinyint] NOT NULL,
	[Deal Price] [decimal](38, 20) NOT NULL,
	[Show Deal Lines] [int] NOT NULL,
	[Price Group Validation] [int] NOT NULL,
	[Sales Type Filter] [nvarchar](250) NOT NULL,
	[When Deal Pressed] [int] NOT NULL,
	[Explanatory Header Text] [nvarchar](30) NOT NULL,
	[OK Pressed Action] [int] NOT NULL,
	[Selection Type] [int] NOT NULL,
	[Quantity Handling] [int] NOT NULL,
	[Single Mod_ Switch in Pop-up] [int] NOT NULL,
	[Deal Lines Printing] [int] NOT NULL,
	[Customer Disc_ Group] [nvarchar](20) NOT NULL,
	[Member Value] [nvarchar](10) NOT NULL,
	[Discount Tracking No_] [nvarchar](10) NOT NULL,
	[Coupon Code] [nvarchar](10) NOT NULL,
	[Coupon Qty Needed] [decimal](38, 20) NOT NULL,
	[Member Type] [int] NOT NULL,
	[Member Attribute] [nvarchar](10) NOT NULL,
	[Member Attribute Value] [nvarchar](30) NOT NULL,
	[Disc_ Amount from Std_ Price] [decimal](38, 20) NOT NULL,
	[Block Sales Commission] [tinyint] NOT NULL,
	[Block Manual Price Change] [tinyint] NOT NULL,
	[Block InfoCode Discount] [tinyint] NOT NULL,
	[Block Line Discount Offer] [tinyint] NOT NULL,
	[Block Total Discount Offer] [tinyint] NOT NULL,
	[Block Tender Type Discount] [tinyint] NOT NULL,
	[Block Member Points] [tinyint] NOT NULL,
	[Buyer ID] [nvarchar](50) NOT NULL,
	[Buyer Group Code] [nvarchar](10) NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$Offer$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Offer$5ecfc871-5d82-43f1-9c54-59685e82318d$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END
SET @i = @i+1
END